package com.tqjc.system.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tqjc.system.common.constant.GlobalConstant;
import com.tqjc.system.common.controller.BaseController;
import com.tqjc.system.common.entity.VO.AlarmInfoVO;
import com.tqjc.system.common.entity.VO.StatDataVO;
import com.tqjc.system.common.enums.AlarmStatusEnum;
import com.tqjc.system.common.enums.TaskStatusEnum;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.util.DateUtils;
import com.tqjc.system.core.util.ResponseUtil;
import com.tqjc.system.system.entity.SysAlarmInfoEntity;
import com.tqjc.system.system.service.SysAlarmInfoService;
import com.tqjc.system.system.wrapper.AlarmInfoWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description: 告警信息控制层
 * @Author: 86195
 * @Date: 2024/1/4 10:39
 **/
@Controller
@RequestMapping("/sys/alarm")
@Slf4j
public class SysAlarmInfoController extends BaseController {

    @Autowired
    private SysAlarmInfoService alarmInfoService;


    @RequestMapping("/index")
    public String toIndex(){
        return "sys/alarm/alarm_index.html";

    }


    @RequestMapping("/listInfo")
    public void listInfo(AlarmInfoVO alarmInfoVO, PageInfo<SysAlarmInfoEntity> page, HttpServletResponse response){
        log.info("查询告警信息接口开始");
        initDate(alarmInfoVO);
        IPage<SysAlarmInfoEntity> lstResult = alarmInfoService.getListByPage(page.getPageEntity(),alarmInfoVO);
        ResponseUtil.write(response, new AlarmInfoWrapper(lstResult).wrap());
        log.info("查询告警信息接口结束");

    }


    @RequestMapping("/taskExecutionAlarmInfo")
    public void taskExecutionAlarmInfo(AlarmInfoVO alarmInfoVO,PageInfo<SysAlarmInfoEntity> page,HttpServletResponse response){
        if(GlobalConstant.VISIT_MODEL_FLAG){
            //参观模式下，异常信息不展示
            List<SysAlarmInfoEntity> list = null;
            ResponseUtil.write(response, new AlarmInfoWrapper(list).wrap());
            return;
        }

        log.info("taskExecutionAlarmInfo接口开始");
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(3);
        alarmInfoVO.setHappenStartDate(localDateTime);
        alarmInfoVO.setStatusList(Arrays.asList(AlarmStatusEnum.ALARM_STATUS_ERROR.getStatus(),AlarmStatusEnum.ALARM_STATUS_INIT.getStatus()));
        IPage<SysAlarmInfoEntity> res = alarmInfoService.getListByPage(page.getPageEntity(), alarmInfoVO);
        ResponseUtil.write(response, new AlarmInfoWrapper(res).wrap());
        log.info("taskExecutionAlarmInfo接口结束");

    }


    @RequestMapping("/idKey")
    public String getDetailInfo(String  idKey, Model model){
        log.info("查询告警信息详情接口开始，idkey:{}",idKey);
        SysAlarmInfoEntity alarmInfo = alarmInfoService.getById(idKey);
        alarmInfo.setStatus(AlarmStatusEnum.getDescByStatus(alarmInfo.getStatus()));
        model.addAttribute("alarmInfo",alarmInfo);
        log.info("查询告警信息详情接口结束，idkey:{}",idKey);
        return "sys/alarm/alarmInfoDetail.html";

    }

    @RequestMapping("/update")
    @ResponseBody
    public AjaxJson update(AlarmInfoVO alarmInfoVO){
        AjaxJson ajaxJson = new AjaxJson();
        Long idKey = alarmInfoVO.getIdKey();
        try {
            log.info("告警信息更改状态接口开始，idkey:{}",idKey);
            SysAlarmInfoEntity alarmInfoEntity = SysAlarmInfoEntity.builder().status(alarmInfoVO.getStatus()).idKey(idKey).build();
            alarmInfoService.updateById(alarmInfoEntity);
            log.info("告警信息更改状态接口结束，idkey:{}",idKey);
            ajaxJson.setMsg("更改状态成功");
        } catch (Exception e){
            log.error("告警信息更改状态接口出现异常，idkey:{}",idKey,e);
            ajaxJson.setMsg("更改状态失败,请联系管理员");

        }
        return ajaxJson;
    }


    @RequestMapping("/getStatData")
    @ResponseBody
    public AjaxJson getStatData(AlarmInfoVO alarmInfoVO){
        AjaxJson ajaxJson = new AjaxJson();
        Long idKey = alarmInfoVO.getIdKey();
        try {
            log.info("告警信息统计数据接口开始");
            initDate(alarmInfoVO);
            List<StatDataVO> list = alarmInfoService.getStatData(alarmInfoVO);
            log.info("告警信息统计数据接口结束");
            ajaxJson.setObj(list);
        } catch (Exception e){
            log.error("告警信息统计数据接口出现异常，idkey:{}",idKey,e);
            ajaxJson.setSuccess(false);
            ajaxJson.setMsg("告警信息统计数据接口出现异常");

        }
        return ajaxJson;
    }



    private void initDate(AlarmInfoVO alarmInfoVO){
        if(StringUtils.isNotEmpty(alarmInfoVO.getHappenEndDateStr())){
            String happenEndDateStr = alarmInfoVO.getHappenEndDateStr() + " 23:59:59";
            alarmInfoVO.setHappenEndDate(DateUtils.getLocalDateTime(happenEndDateStr));
        }
        if(StringUtils.isNotEmpty(alarmInfoVO.getHappenStartDateStr())){
            String happenStartDateStr = alarmInfoVO.getHappenStartDateStr() + " 00:00:00";

            alarmInfoVO.setHappenStartDate(DateUtils.getLocalDateTime(happenStartDateStr));

        }

    }






}
